<!--
 * @Author: 哈尔滨大拐 3181845089@qq.com
 * @Date: 2022-07-10 11:05:31
 * @LastEditors: 哈尔滨大拐 3181845089@qq.com
 * @LastEditTime: 2022-07-10 12:57:13
 * @FilePath: \mygit\实验\19-随机背景颜色.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: #959054;
            color: #fff;
            text-align: center;
            padding-top: 200px;
        }

        h1 {
            font-size: 40px;
        }

        button {
            color: #fff;
            width: 100px;
            height: 40px;
            background: transparent;
            border: 2px solid #fff;
            border-radius: 20px;
            font-size: 15px;
            font-weight: 600;
            cursor: pointer;
        }

        #end {
            margin-left: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>#959054</h1>
        <button id="start">更换颜色</button>
        <button id="end">停止</button>
    </div>

    <script>
        const startBtn = document.getElementById('start')
        const endBtn = document.getElementById('end')

        const body = document.querySelector('body')
        const h1 = document.querySelector('h1')



        let timer
        startBtn.addEventListener('click', function () {
            clearInterval(timer)
            let color
            timer = setInterval(() => {
                color = getColor()
                body.style.background = color
                h1.innerHTML = color
            }, 1000)
        })



        endBtn.addEventListener('click', function () {
            clearInterval(timer)
        })



        function getColor() {
            function getNum16() {
                return Math.floor(Math.random() * 256).toString(16)
            }
            return '#' + getNum16() + getNum16() + getNum16()
        }
        console.log(getColor());

    </script>
</body>

</html>